package idds;

import system.Message;
import Main.Parameters;
import plugin.SystemInterface;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * Help for Ice Dragon Delivery System
 * @author zem
 */
public class help implements SystemInterface {

    boolean quit;
    String Next;
    String[] Visited;
    
    int VisitedPosition;
    int VisitedLength;
    String[] Jump;

    public void start() {
        quit = false;
        Next = "main";

    VisitedPosition=-1;
    VisitedLength=0;
        Visited = new String[10];
       
        do {
            
            ReadParagraf();
            AddToVisitedList(Next);
            
            //read input
            ReadInput();
        } while (!quit);


    }

    public void interrupt() {
        quit = true;
    }

    void ReadInput() {
        Next = null;
        BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));
        try {
            String in = inp.readLine();
            if (in != null && in.length() > 0) {
                if (in.equals("exit") | in.equals("quit")) { //if exit parameter
                    interrupt();
                } else if (in.equals("back")) {
                    if(VisitedPosition>0){
                    VisitedPosition--;
                    Next=Visited[VisitedPosition];
                    }else System.out.println(new Message("ev0005").message);
                } else if (in.equals("next")) {
                    if(VisitedPosition<VisitedLength-1){
                    Next=Visited[VisitedPosition+1];
                    VisitedPosition++;

                    } else {
                        System.out.println(new Message("er0004").message);
                    }
                }   else if (in.equals("main")) {
                    Next="main";
                     }   else if (in.equals("help")) {
                    Next="help";

                }   else if (in.equals("0") | in.equals("1") | in.equals("2") | in.equals("3") | in.equals("4") | in.equals("5") | in.equals("6") | in.equals("7") | in.equals("8") | in.equals("9")){
                //looking in-to jump
                    if(Jump[Integer.parseInt(in)]!=null){
                    Next=Jump[Integer.parseInt(in)];
                    } else {
                        System.out.println(new Message("er0005").message);
                    }
                } else {
                System.out.println(new Message("er0005").message);
                }


            }
        } catch (Exception er) {
        }
    }

    void AddToVisitedList(String paragraf) { //add to visited list
        if (VisitedPosition < 9) {
            if(Visited[VisitedPosition+1]!=null){
            if (!Visited[VisitedPosition+1].equals(paragraf)) {
                if(VisitedPosition==(VisitedLength-1)){
                Visited[VisitedPosition+1] = paragraf;
                VisitedPosition++;
                VisitedLength=VisitedPosition+1;
                }
            }else {
                    VisitedPosition++;
                }
            } else {
              if(VisitedPosition==VisitedLength-1){
                Visited[VisitedPosition+1] = paragraf;
                VisitedPosition++;
                VisitedLength=VisitedPosition+1;
                }

            }
        }else{
            for(int i=0;i<9;i++){
            Visited[i]=Visited[i+1];
            }
            Visited[VisitedPosition] = paragraf;

        }
    }

    void ReadParagraf() { //reading paragraf
        BufferedReader in;
        try {

            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(new File("locale" + File.separatorChar + "help." + Parameters.Language + ".xml"));

            // normalize text representation
            doc.getDocumentElement().normalize();
            NodeList MainPart = doc.getElementsByTagName(Next);
            Node firstNode = MainPart.item(0);
            if (firstNode.getNodeType() == Node.ELEMENT_NODE) {


                Element firstElement = (Element) firstNode;

                boolean flag = true; //mean that everithing is OK

                //----description
                NodeList nodelist = firstElement.getElementsByTagName("message");

                int length = nodelist.getLength();
                System.out.println("================================================================================");
                for (int i = 0; i < length; i++) { //printing messages
                    Element nelement = (Element) nodelist.item(i);

                    NodeList textList = nelement.getChildNodes();
                    String temp = ((Node) textList.item(0)).getNodeValue().trim();
                    char[] chars = temp.toCharArray();
                    temp = "";
                    for (int j = 1; j < chars.length - 1; j++) {
                        temp += chars[j];
                    }
                    System.out.println(temp);
                }
                System.out.println("================================================================================");
                //checking jump cases
                Jump = new String[9]; //max number of jumps 10
                for(int j=0;j<10;j++){
                nodelist = firstElement.getElementsByTagName("jump_"+j);
                length = nodelist.getLength();
                if (length>0){
                 Element nelement = (Element) nodelist.item(0);

                    NodeList textList = nelement.getChildNodes();
                    Jump[j] = ((Node) textList.item(0)).getNodeValue().trim();
                }
                }
            }
        } catch (Exception er) {
            
        }
    }
}

